a.Storage.state.Theme = {};
//SelectH5 begin
a.SelectH5 = function(items,index,changeFn,args){
    var _this = this;
    args = args || {};
    var itemsHeight = a.computPX(args.itemsHeight || 600);
    var itemHeight = a.computPX(args.itemHeight || 60);
    this.dom = a.creatElement('div',{
        style:{
            height:itemsHeight + 'px',
            overflow:'hidden'
        }
    });
    var content = a.creatElement('div',{
        style:{
            transition:'all 0.3s',
            marginTop:'0px'
        },
        parent:this.dom
    })
    var active = '';
    this.selectedStyle = args.selectedStyle || function(dom,bool){
        if(dom){
            if(bool){
                dom.style.opacity = 1;
                dom.style.fontWeight = 'bold';
            }else{
                dom.style.opacity = 0.5;
                dom.style.fontWeight = 'normal';
            }
        }
    }
    var end = false;
    this.run = function(n,step){
        if(active) this.selectedStyle(active);
        index = index > items.length - 1 ? items.length - 1 : index;
        index = index < 0 ? 0 : index;
        active = content.childNodes[index];
        var top = index * itemHeight - itemsHeight / 2;
        content.style.marginTop = -top + 'px';
        this.selectedStyle(active,true);
        changeFn(index);
        if(end) {
            if(args.endFn) args.endFn(index);
            end = false;
        }
        if(n && n > 0) {
            setTimeout(function(){
                index = index + step;
                if(n == 1) end = true;
                _this.run(n - 1,step);
            },1000 / 60)
        }
    }
    this.builded = false;
    this.build = function(_items,_index){
        if(_items && _index != undefined){
            items = _items;
            index = _index;
            this.builded = false;
        }
        if(!this.builded){
            active = '';
            content.innerHTML = '';
            for(var i = 0; i < items.length; i ++) {
                (function(i){
                    content.appendChild(items[i]);
                    if(args.click) {
                        a.on(items[i],'click',function(){
                            var n = Math.abs(i - index);
                            _this.run(n,(i - index) / n);
                        })
                    }
                })(i)
            }
            var y0 = 0,index0 = index,z0 = 0;
            function show(y,z,force){
                y = y0 - y || 0;
                index = index0 + parseInt(y / itemHeight);
                z = z - z0;
                var n = 0;
                if(z > 50 && Math.abs(y) > 0 && z < 370) n = parseInt(z / 120);
                _this.run(n,y < 0 ? -1 : 1);
            }
            show();
            if(!this.dom.binded) {
                var bindType = a.Storage.get('Theme').SelectH5_bindType || 'touch';
                if(bindType == 'touch'){
                    this.dom.addEventListener('touchstart', function(e){
                        e.preventDefault();
                        e.stopPropagation();
                        index0 = index;
                        y0 = e.changedTouches[0].clientY;
                        z0 = new Date();
                    }, false);
                    this.dom.addEventListener('touchmove', function(e){
                        e.preventDefault();
                        e.stopPropagation();
                        show(e.changedTouches[0].clientY);
                    }, false);
                    this.dom.addEventListener('touchend', function(e){
                        e.preventDefault();
                        var now = new Date();
                        if(now - z0 >= 370) end = true;
                        show(e.changedTouches[0].clientY,now,e.changedTouches[0].force);
                    }, false);
                }else{
                    a.on(this.dom,'DOMMouseScroll',function(event){
                        event.preventDefault();
                        event.stopPropagation();
                        _this.run(1,event.detail / Math.abs(event.detail));
                    })
                    a.on(this.dom,'mousewheel',function(event){
                        event.preventDefault();
                        event.stopPropagation();
                        _this.run(1,-event.wheelDelta / Math.abs(event.wheelDelta));
                    })
                }
                this.dom.binded = true;
            }
        }
        this.builded = true;
    }
}
//SelectH5 end
//Picker begin
a.Picker = function(){
    this.dom = a.body;
    this.cover = null;
    this.callback = null;
    this.rc = null;
    this.content = null;
    var theme = a.Storage.state.Theme.Picker || {};
    this.addStyles = function(){
        var rc = '';
        for(var i = 0; i < arguments.length; i ++) rc += arguments[i] || '';
        return a.computStyle(rc);
    }
    this.remove = function(){
        if(this.closeFn) this.closeFn();
        if(this.cover) this.dom.removeChild(this.cover);
        if(this.content) this.dom.removeChild(this.content);
        this.cover = null;
        this.content = null;
    }
    this.build = function(o,args){
        if(this.cover) return false;
        var _this = this;
        args = args || {};
        var box = '';
        if(!args.box){
            this.cover = a.creatElement('div',{
                style:this.addStyles('position:fixed;left:0px;top:0px;right:0px;bottom:0px;font-size:32px;background:#000000;opacity:0.5;filter:alpha(opacity=50);zIndex:{zIndex};'._eval({zIndex:a.zIndex()}),theme.pickCover,args.pickCover),
                className:'pickCover',
                parent:this.dom,
                onclick:function(event){
                    _this.callback({result:'close'});
                    _this.remove();
                }
            });
            box = a.creatElement('div',{
                className:'pickBox',
                style:this.addStyles('user-select:none;position:absolute;left:0px;bottom:0px;right:0px;background:#ffffff;zIndex:{zIndex};'._eval({zIndex:a.zIndex()}),theme.pickBox,args.pickBox),
                parent:this.dom
            })
            var title = a.creatElement('div',{
                className:'pickTitle',
                style:this.addStyles('height:80px;display:flex;justify-content:space-between;background:#F7FBFD;',theme.pickTitle,args.pickTitle),
                parent:box
            })
            var pickBtn = 'color:#03A9F4;fontSize:28px;border:1px solid #F7FBFD;width:100px;height:48px;line-height:48px;text-align:center;margin-top:16px;border-radius:6px;';
            var cancel = a.creatElement('div',{
                style:this.addStyles(pickBtn,'margin-left:32px;',theme.pickBtn,theme.btnCancel,args.btnCancel),
                innerHTML:args.cancelText || '取消',
                className:'pickBtn',
                onclick:function(){
                    _this.callback({result:'cancel'});
                    _this.remove();
                },
                parent:title
            })
            var titleText = a.creatElement('div',{
                style:this.addStyles('flex:1;',theme.titleText,args.titleText),
                innerHTML:args.title || '',
                className:'titleText',
                parent:title
            })
            if(args.clear){
                a.creatElement('div',{
                    style:this.addStyles(pickBtn,'margin-right:0px;',theme.pickBtn,theme.btnClear,args.btnClear),
                    innerHTML:'清空',
                    className:'pickBtn',
                    onclick:function(){
                        _this.callback({result:'clear'});
                        _this.remove();
                    },
                    parent:title
                })
            }
            var selected = a.creatElement('div',{
                style:this.addStyles(pickBtn,'margin-right:32px;',theme.pickBtn,theme.btnSelect,args.btnSelect),
                innerHTML:args.selectedText || '确定',
                className:'pickBtn',
                onclick:function(){
                    _this.callback(_this.rc);
                    _this.remove();
                },
                parent:title
            })
            this.content = box;
        }else{
            box = args.box;
        }
        a.creatElement('div',{className:'pickContent',parent:box,style:this.addStyles(theme.pickContent,args.pickContent)}).appendChild(o);
    }
    this.itemStyle = {};
    this.createDomItems = function(items,nameEval){
        var _items = [];
        var style = 'height:60px;line-height:60px;overflow:hidden;text-align:center;color:#000000;opacity:0.5;transition:all 0.3s;'
        style = this.addStyles(style,theme.pickItem,this.pickItem);
        for(var i = 0; i < items.length;i ++){
            var name = items[i].name ? items[i].name : items[i];
            if(nameEval) {
                if(typeof nameEval == 'string') {
                    name = nameEval.replace(/{value}/g,name);
                }else if(typeof nameEval == 'function'){
                    name = nameEval(name);
                }
            }
            _items.push(a.creatElement('div',{
                innerHTML:name,
                style:style
            }))
        }
        return _items;
    }
    var selectArgs = theme.Select || {};
    this.args = {};
    this.pick = function(args,callback){
        var _this = this;
        args = args || {};
        args.index = args.index || 0;
        this.args = args;
        _this.callback = callback;
        this.itemStyle = args.itemStyle || {};
        var domItem = new a.SelectH5(this.createDomItems(args.items),args.index,function(index){
            _this.rc = {result:'success',data:_this.args.items[index],index:index};
        },a.merge(a.clone(selectArgs),args));
        domItem.build();
        this.build(domItem.dom,args);
        return domItem;
    }
    this.pickColor = function(args,calllback){
        args.colors = {
            '#ff0000':'红色',
            '#0521fb':'蓝色',
            '#198f10':'绿色',
            '#f5f411':'黄色',
            '#000000':'黑色'
        };
        args.items = [];
        for(var key in args.colors){
            args.items.push({
                value:key,
                name:'<div style="width:50%;margin:0 auto;color:#ffffff;height:100%;padding:10px;background:' + key + '">' + args.colors[key] + '</div>'
            })
        }
        this.pick(args,calllback);
      }
    this.createItems = function(min,max){
        var items = [],index ;
        for(var i = min; i <= max; i ++) items.push(i);
        return items;
    }
    this.numFormat = function(n){
        return typeof n == 'string' ? (n.charAt(0) == 0 ? n.charAt(1) : n) : (n > 9 ? n.toString() : '0' + n);
    }
    this.pickTime = function(args,callback){
        var _this = this;
        args = args || {};
        args.type = args.type || 3;
        this.args = args;
        _this.callback = callback;
        var o = {};
        o.build = function(_args){
            if(_args) for(var k in _args) args[k] = _args[k];
            var box = a.creatElement('div',{
                style:{
                    display:'flex',
                    flex:1
                }
            })
            if(args.box) args.box.innerHTML = '';
            _args = {endFn:args.endFn};
            var now = new Date();
            var value = args.value ? args.value.split(':') : [now.getHours(),now.getMinutes(),now.getSeconds()];
            value[0] = _this.numFormat(value[0]);
            var hItems = _this.createItems(0,23);
            var hIndex = value[0] - hItems[0];
            var mItems = _this.createItems(0,59);
            var mIndex = value[1] - mItems[0];
            var sItems = _this.createItems(0,59);
            var sIndex = value[2] - sItems[0];

            var hDomItems = new a.SelectH5(_this.createDomItems(hItems,'{value} 时'),hIndex,function(index){
                hIndex = index;
                toString();
            },a.merge(a.clone(selectArgs),_args));
            hDomItems.build();
            hDomItems.dom.style.flex = 1;
            hDomItems.dom.style.float = 'left';
            hDomItems.dom.style.width = '100%';
            box.appendChild(hDomItems.dom);
            o.run = function(value_){
                var n0 = _this.numFormat(value_) - hIndex;
                hDomItems.run(Math.abs(n0),n0 / Math.abs(n0));
            }
            if(args.type > 1){
                value[1] = _this.numFormat(value[1]);
                var mDomItems = new a.SelectH5(_this.createDomItems(mItems,'{value} 分'),mIndex,function(index){
                    mIndex = index;
                    toString();
                },a.merge(a.clone(selectArgs),_args));
                mDomItems.build();
                mDomItems.dom.style.flex = 1;
                mDomItems.dom.style.float = 'left';
                mDomItems.dom.style.width = '50%';
                hDomItems.dom.style.width = '50%';
                box.appendChild(mDomItems.dom);
                o.run = function(value_){
                    value_ = value_.split(':')
                    var n0 = _this.numFormat(value_[0]) - hIndex;
                    hDomItems.run(Math.abs(n0),n0 / Math.abs(n0));
                    var n1 = _this.numFormat(value_[1]) - mIndex;
                    mDomItems.run(Math.abs(n1),n1 / Math.abs(n1));
                }
            }
            if(args.type > 2){
                value[2] = _this.numFormat(value[2]);
                var sDomItems = new a.SelectH5(_this.createDomItems(sItems,'{value} 秒'),sIndex,function(index){
                    sIndex = index;
                    toString();
                },a.merge(a.clone(selectArgs),_args));
                sDomItems.build();
                sDomItems.dom.style.flex = 1;
                sDomItems.dom.style.float = 'left';
                sDomItems.dom.style.width = '33%';
                mDomItems.dom.style.width = '33%';
                hDomItems.dom.style.width = '33%';
                box.appendChild(sDomItems.dom);
                o.run = function(value_){
                    value_ = value_.split(':')
                    var n0 = _this.numFormat(value_[0]) - hIndex;
                    hDomItems.run(Math.abs(n0),n0 / Math.abs(n0));
                    var n1 = _this.numFormat(value_[1]) - mIndex;
                    mDomItems.run(Math.abs(n1),n1 / Math.abs(n1));
                    var n2 = _this.numFormat(value_[2]) - sIndex;
                    sDomItems.run(Math.abs(n1),n1 / Math.abs(n1));
                }
            }
            _this.build(box,args);
            function toString(){
                var rc = _this.numFormat(hItems[hIndex]);
                if(args.type > 1) rc += ':' + _this.numFormat(mItems[mIndex]);
                if(args.type > 2) rc += ':' + _this.numFormat(sItems[sIndex]);
                _this.rc = {result:'success',data:rc};
                if(args.change) args.change(_this.rc);
            }
        }
        o.build();
        return o;
    }
    this.pickDate = function(args,callback){
        var _this = this;
        _this.callback = callback;
        args = args || {};
        args.type = args.type || 3;//类型1-3 分别对应到年月日
        this.args = args;
        var o = {};
        o.build = function(_args){
            if(_args) for(var k in _args) args[k] = _args[k];
            var now = new Date();
            var defaultTime = [[1900,1,1],[2050,12,31]];
            var box = a.creatElement('div',{
                style:{
                    display:'flex',
                    flex:1
                }
            })
            if(args.box) args.box.innerHTML = '';
            _args = {endFn:args.endFn};
            switch(args.type){
                case 1:
                    var years = _this.createItems(args.min || defaultTime[0][0],args.max || defaultTime[1][0]);
                    var yearIndex = (args.value || now.getFullYear()) - years[0];
                    var yearDomItems = new a.SelectH5(_this.createDomItems(years,'{value} 年'),yearIndex,function(index){
                        yearIndex = index;
                        _this.rc = {result:'success',data:years[yearIndex],index:yearIndex};
                        if(args.change) args.change.call({year:[yearIndex,years]},_this.rc);
                    },a.merge(a.clone(selectArgs),_args))
                    yearDomItems.build();
                    yearDomItems.dom.style.flex = 1;
                    box.appendChild(yearDomItems.dom);
                    o.run = function(value_){
                        var index_ = _this.numFormat(value_) - years[0];
                        var n = index_ - yearIndex;
                        yearDomItems.run(Math.abs(n),n / Math.abs(n));
                    }
                    break;
                case 2:
                    var value = args.value ? args.value.split('-') : [now.getFullYear(),now.getMonth() + 1];
                    value[1] = _this.numFormat(value[1]);
                    var min = args.min ? args.min.split('-') : [defaultTime[0][0],defaultTime[0][1]];
                    var max = args.max ? args.max.split('-') : [defaultTime[1][0],defaultTime[1][1]];
                    var yearsItems = _this.createItems(min[0],max[0]);
                    var yearsIndex = value[0] - yearsItems[0];
                    var monthItems = _this.createItems(1,12);
                    var monthIndex = value[1] - monthItems[0];
                    var changeContext = {year:[yearIndex,yearsItems],month:[monthIndex,monthItems]};
                    var yearDomItems = new a.SelectH5(_this.createDomItems(yearsItems,'{value} 年'),yearsIndex,function(index){
                        yearsIndex = index;
                        _this.rc = {result:'success',data:yearsItems[yearsIndex] + '-' + _this.numFormat(monthItems[monthIndex])};
                        if(args.change) args.change.call(changeContext,_this.rc);
                    },a.merge(a.clone(selectArgs),_args))
                    yearDomItems.build();
                    var monthDomItems = new a.SelectH5(_this.createDomItems(monthItems,'{value} 月'),monthIndex,function(index){
                        monthIndex = index;
                        _this.rc = {result:'success',data:yearsItems[yearsIndex] + '-' + _this.numFormat(monthItems[monthIndex])};
                        if(args.change) args.change.call(changeContext,_this.rc);
                    },a.merge(a.clone(selectArgs),_args))
                    monthDomItems.build();
                    yearDomItems.dom.style.flex = 1;
                    yearDomItems.dom.style.width = '50%';
                    yearDomItems.dom.style.float = 'left';
                    box.appendChild(yearDomItems.dom);
                    monthDomItems.dom.style.flex = 1;
                    monthDomItems.dom.style.width = '50%';
                    monthDomItems.dom.style.float = 'left';
                    box.appendChild(monthDomItems.dom);
                    o.run = function(value_){
                        value_ = value_.split('-');
                        var n0 = _this.numFormat(value_[0]) - yearIndex;
                        yearDomItems.run(Math.abs(n0),n0 / Math.abs(n0));
                        var n1 = _this.numFormat(value_[1]) - monthIndex;
                        monthDomItems.run(Math.abs(n1),n1 / Math.abs(n1));
                    }
                    break;
                default:
                    var value = args.value ? args.value.split('-') : [now.getFullYear(),now.getMonth() + 1,now.getDate()];
                    value[1] = _this.numFormat(value[1]);
                    value[2] = _this.numFormat(value[2]);
                    var min = args.min ? args.min.split('-') : defaultTime[0];
                    var max = args.max ? args.max.split('-') : defaultTime[1];
                    var yearsItems = _this.createItems(min[0],max[0]);
                    var yearsIndex = value[0] - yearsItems[0];
                    var monthItems = _this.createItems(1,12);
                    var monthIndex = value[1] - monthItems[0];
                    var dateItems = _this.createItems(1,30);
                    var dateIndex = value[2] - dateItems[0];
                    var month12 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                    var changeContext = {year:[yearIndex,yearsItems],month:[monthIndex,monthItems],date:[dateIndex,dateItems]};
                    var dateDomItems = new a.SelectH5(_this.createDomItems(dateItems,'{value} 日'),dateIndex,function(index){
                        dateIndex = index;
                        _this.rc = {result:'success',data:yearsItems[yearsIndex] + '-' + _this.numFormat(monthItems[monthIndex]) + '-' + _this.numFormat(dateItems[dateIndex])};
                        if(args.change) args.change.call(changeContext,_this.rc);
                    },a.merge(a.clone(selectArgs),_args))
                    var monthDomItems = new a.SelectH5(_this.createDomItems(monthItems,'{value} 月'),monthIndex,function(index){
                        monthIndex = index;
                        dateItems = _this.createItems(1,month12[monthIndex]);
                        dateIndex = dateIndex > dateItems[dateItems.length - 1] ? dateItems[dateItems.length - 1] : dateIndex;
                        dateDomItems.build(_this.createDomItems(dateItems,'{value} 日'),dateIndex);
                    },a.clone(selectArgs))
                    var yearDomItems = new a.SelectH5(_this.createDomItems(yearsItems,'{value} 年'),yearsIndex,function(index){
                        yearsIndex = index;
                        month12[1] = (yearsItems[yearsIndex] % 4 == 0 && yearsItems[yearsIndex] % 100 != 4) || yearsItems[yearsIndex] % 400 == 0 ? 29 : 28;
                        monthDomItems.build();
                        _this.rc = {result:'success',data:yearsItems[yearsIndex] + '-' + _this.numFormat(monthItems[monthIndex]) + '-' + _this.numFormat(dateItems[dateIndex])};
                        if(args.change) args.change.call(changeContext,_this.rc);
                    },a.clone(selectArgs))
                    yearDomItems.build();
                    yearDomItems.dom.style.flex = 1;
                    yearDomItems.dom.style.width = '33%';
                    yearDomItems.dom.style.float = 'left';
                    box.appendChild(yearDomItems.dom);
                    monthDomItems.dom.style.flex = 1;
                    monthDomItems.dom.style.width = '33%';
                    monthDomItems.dom.style.float = 'left';
                    box.appendChild(monthDomItems.dom);
                    dateDomItems.dom.style.flex = 1;
                    dateDomItems.dom.style.width = '33%';
                    dateDomItems.dom.style.float = 'left';
                    box.appendChild(dateDomItems.dom);
                    o.run = function(value_){
                        o.build({value:value_});
                    }
                    break;
            }
            _this.build(box,args);
        }
        o.build();
        return o;
    }
    this.picks = function(args,callback){
        args = args || {};
        var _this = this;
        var box = a.creatElement('div',{
            style:{
                display:'flex'
            }
        })
        this.rc = {result:'success'};
        var pickers = [];
        var pickDoms = [];
        var arr = args.args;
        for(var i = 0; i < arr.length; i ++){
            (function(i){
                arr[i].box = a.creatElement('div',{
                    style:a.merge({
                        flex:1,
                        float:'left',
                        width: (100 / arr.length) + '%'
                    }, arr[i].styles || {}),
                    parent:box
                })
                arr[i].endFn = function(index){
                    (function(i,index){
                        if(args.endFn) args.endFn(i,pickers,index,pickDoms);
                    })(i,index)
                }
                pickers[i] = new a.Picker();
                pickDoms[i] = pickers[i][arr[i].pickType](arr[i]);
            })(i)
        }
        _this.callback = function(ret){
            if(ret.result == 'success'){
                var rc = [];
                for(var i = 0; i < pickers.length; i ++) rc.push(pickers[i].rc.data);
                callback({result:'success',data:rc});
            }else{
                callback(ret);
            }
        }
        this.build(box,args);
        return pickers;
    }
}
//Picker end
//Toask begin
a.Toask = {
    cover:null,
    content:null,
    contentCover:null,
    dom:a.body,
    buildCover:function(coverStyle,toaskArgs){
        coverStyle = coverStyle || {};
        if(this.cover) this.close();
        if(toaskArgs.mask){
            coverStyle.background = '#000000';
            coverStyle.opacity = '0.01';
            coverStyle.filter = 'alpha(opacity=1)';
        }else{
            coverStyle.background = 'none';
        }
        this.cover = a.creatElement('div',{
            style:a.merge({
                position:'fixed',
                left:'0px',
                top:'0px',
                right:'0px',
                bottom:'0px',
                zIndex: a.zIndex()
            },coverStyle),
            parent:this.dom
        })
        if(typeof toaskArgs.append == 'function') {
            var nextElement = toaskArgs.append.call(this);
            if(nextElement) this.box.appendChild(nextElement);
        }
    },
    buildBox:function(args,defaultTitle){
        args = args || {};
        var size = args.size || a.computPX(244) + 'px';
        size = {width:size,height:size};
        args.title = args.title || defaultTitle || '';
        switch(args.icon){
            case 'loading':
                args.icon = '<i style="width:96px;height:96px;" class="loading"></i>';
                break;
            case 'none':
                args.icon = '';
                break;
            default:
                args.icon = args.icon || 'success_no_circle';
                args.icon = '<i style="font-size:96px;margin-bottom:96px;" class="icon-' + args.icon.replace(/_/g,'-') + '"></i>';
                break;
        }
        if(args.image) args.icon = '<image src="{image}" style="width:96px;height:96px;" />';
        var iconDisplay = 'auto';
        if(!args.icon){
            iconDisplay = 'none';
            size.height = 'auto';
        }
        var styles = a.merge({
            width:size.width,
            height:size.height,
            color:'#ffffff',
            textAlign:'center',
            position:'fixed',
            left:'50%',
            top:'50%',
            marginLeft:'-' + (size.width.replace('px','') / 2) + 'px',
            transform: 'translate(0, -50%)',
            background:'#000000',
            opacity:'0.8',
            borderRadius:a.computPX(10) + 'px',
            filter:'alpha(opacity=80)',
            zIndex:a.zIndex()
        },args.boxStyle || {})
        this.contentCover = a.creatElement('div',{
            parent:this.dom,
            style:styles
        });
        this.content = a.creatElement('div',{
            parent:this.dom,
            innerHTML:a.computStyle('<div style="height:96px;margin:16px;padding-top:32px;display:{iconDisplay}">{icon}</div><div style="padding:16px;">{title}</div>'._eval({
                icon:args.icon,
                title:args.title,
                image:args.image,
                iconDisplay:iconDisplay
            })),
            style:a.merge(styles,{
                background:'none',
                opacity:'1',
                filter:'alpha(opacity=100)',
                zIndex:a.zIndex()
            })
        });
        this.contentCover.style.height = a.getBoundingClientRect(this.content).height + 'px';
        return this.content;
    },
    open:function(args){
        args = a.merge(a.clone(a.Storage.get('Theme').Toask || {}),args || {});
        var _this = this;
        if(args.loading){
            args.icon = 'loading';
            this.buildCover(args.cover || {},args);
            this.buildBox(args,'加载中...');
        }else{
            this.buildCover(args.cover || {},args);
            this.buildBox(args);
            setTimeout(function(){
                _this.close(true);
            },args.duration || 3000)
        }
    },
    close:function(duration){
        if(this.cover) this.dom.removeChild(this.cover);
        this.cover = null;
        if(this.content) this.dom.removeChild(this.content);
        this.content = null;
        if(this.contentCover) this.dom.removeChild(this.contentCover);
        this.contentCover = null;
    }
}
//Toask end 
//Modal begin
a.Modal = {
    cover:null,
    content:null,
    item:new a.Item(),
    dom:a.body,
    itemNext:'',
    buildCover:function(coverStyle,toaskArgs){
        coverStyle = coverStyle || {};
        this.cover = a.creatElement('div',{
            style:a.merge({
                position:'fixed',
                left:'0px',
                top:'0px',
                right:'0px',
                bottom:'0px',
                background:'#000000',
                opacity:'0.6',
                filter:'alpha(opacity=60)',
                zIndex: a.zIndex()
            },coverStyle),
            parent:this.dom
        })
        if(typeof toaskArgs.append == 'function') {
            var nextElement = toaskArgs.append.call(this);
            if(nextElement) this.box.appendChild(nextElement);
        }
    },
    buildBox:function(args){
        var _this = this;
        args = args || {};
        args.showCancel = args.showCancel == undefined ? true : args.showCancel;
        args.content = args.content || '';
        var html = '',noTitle_content = 'padding-top:80px;';
        if(a.platform == 'Web'){
            a.Storage.set('pxScale',1);
            if(args.title) html += '<div id="modalTitle" style="height:40px;font-size:18px;font-weight:400;padding:8px 8px;text-align: left;">{title}</div>';
            html += '<div id="modalContent" style="flex:1;text-align: left;font-size: 14px;line-height: 1.3;word-wrap: break-word;word-break: break-all;color: #808080;padding:0px 8px 4px 8px;{noTitle_content}">{content}</div>';
            html += '<div id="modalBtn" style="margin-top:16px;border-top: 1px solid #D5D5D6;padding:8px 16px;text-align:right;">';
            if(args.btns) html += args.btns;
            if(args.showCancel) html += '<span id="modalCancel" style="font-size:16px;margin-right:16px;padding-right:16px;cursor: pointer;border-right: 1px solid #D5D5D6;color:{cancelColor};">{cancelText}</span>';
            html += '<span id="modalConfirm" style="font-size:16px;cursor: pointer;color:{confirmColor};">{confirmText}</span></div>'
            noTitle_content = 'padding-top:24px;'
            var styles = a.merge({
                width:'300px',
                minHeight:'100px',
                background:'#fff',
                color:'#353535',
                textAlign:'center',
                borderRadius:'10px',
                position:'fixed',
                left:'50%',
                top:'50%',
                zIndex:a.zIndex(),
                transform: 'translate(-50%, -50%)',
                display: 'flex',
                flexDirection: 'column'
            },args.boxStyle || {})
        }else{
            if(args.title) html += '<div id="modalTitle" style="height:114px;text-align:center;font-size:36px;font-weight:400;padding:42px 52px 16px 52px;">{title}</div>';
            html += '<div id="modalContent" style="flex:1;font-size: 30px;line-height: 1.3;word-wrap: break-word;word-break: break-all;color: #808080;padding:0px 48px 24px 48px;{noTitle_content}">{content}</div>';
            html += '<div id="modalBtn" style="border-top: 1px solid #D5D5D6;height:96px;display:flex;line-height:96px;">';
            if(args.btns) html += args.btns;
            if(args.showCancel) html += '<span id="modalCancel" style="border-right: 1px solid #D5D5D6;flex:1;color:{cancelColor};">{cancelText}</span>';
            html += '<span id="modalConfirm" style="flex:1;color:{confirmColor};">{confirmText}</span></div>'
            var styles = a.merge({
                width:a.computPX(600) + 'px',
                minHeight:'80px',
                background:'#fff',
                color:'#353535',
                textAlign:'center',
                borderRadius:a.computPX(10) + 'px',
                position:'fixed',
                left:'50%',
                top:'50%',
                zIndex:a.zIndex(),
                transform: 'translate(-50%, -50%)',
                display: 'flex',
                flexDirection: 'column'
            },args.boxStyle || {})
        }
        
        //styles.marginLeft = '-' + styles.width.replace('px','') / 2 + 'px';
        this.content = a.creatElement('div',{
            parent:this.dom,
            innerHTML:a.computStyle(html._eval(a.merge({
                cancelText:'取消',
                cancelColor:'#000000',
                confirmText:'确定',
                confirmColor:'#3cc51f',
                noTitle_content:args.title ? '' : noTitle_content
            },args))),
            style:styles
        })
        if(args.contentDom) document.getElementById('modalContent').appendChild(args.contentDom)
        if(args.showCancel){
            document.getElementById('modalCancel').onclick = function(){
              if(typeof args.success == 'function'){
                if(args.success({cancel:true}) !== false){
                  _this.close();
                }
              }else{
                _this.close();
              }
            }
        }
        document.getElementById('modalConfirm').onclick = function(){
          if(typeof args.success == 'function'){
            if(args.success({confirm:true}) !== false){
              _this.close();
            }
          }else{
            _this.close();
          }
        }
        if(args.styles){
            for(var key in args.styles){
                var elem = document.getElementById(key);
                if(!elem) continue;
                var styles = typeof args.styles[key] == 'string' ? a.styleSheet(args.styles[key]) : args.styles[key];
                for(var k in styles) elem.style[k] = styles[k];
            }
        }
        return this.content;
    },
    modal:function(args){
        args = a.merge(a.clone(a.Storage.get('Theme').Modal || {}),args || {});
        var _this = this;
        this.item._push(function(next){
            _this.buildCover(args.cover || {},args);
            _this.buildBox(args);
            _this.itemNext = next;
        });
        if(!this.item.begined) this.item._do();
    },
    confirm:function(msg,resolve, reject){
        this.modal(a.merge({
            content:msg,
            title:'',
            success:function(ret){
                if(ret.confirm && typeof resolve == 'function') resolve();
                if(ret.cancel && typeof reject == 'function') reject();
            }
        },a.Storage.get('Theme').Modal_confirm || {}));
    },
    alert:function(msg,next){
        this.modal(a.merge({
            showCancel:false,
            content:msg,
            title:'',
            success:next
        },a.Storage.get('Theme').Modal_alert || {}));
    },
    close:function(duration){
        if(this.cover) this.dom.removeChild(this.cover);
        if(this.content) this.dom.removeChild(this.content);
        this.cover = null;
        this.content = null;
        if(this.itemNext) this.itemNext();
    }
}
//Modal end 
//ImgEdit begin
a.ImgEdit = function(args){
    args = args || {};
    var _this = this;
    this.width = args.width;
    this.height = args.height;
    this.maxWidth = args.maxWidth || a.computPX(1000);
    this.maxHeight = args.maxHeight || a.computPX(400);
    this.name = args.name;
    this.radius = args.radius || 0;
    this.id = a.zIndex();
    this.mimeType = args.mimeType || 'png';
    this.compress = args.compress || 0.92;
    this.canvas = '';
    this.ctx = '';
    this.img = new Image();
    this.left = 0;
    this.top = 0;
    this.scale = 1;
    this.fillStyle = args.fillStyle;
    this.fileSize = function(size){
        var unit = "B";
        if(size > 1024){
            size = Math.ceil(size / 1024);
            unit = "Kb";
            if(size > 1024){
                size = parseInt(size / 1024 * 100) / 100;
                unit = "Mb";
            }
        }
        return size + unit;
    }
    this.init = function(args){
        args =  args || {};
        a.merge(this,args);
    }
    this.recommend = function(){
        if(!this.width) {
            if(!this.height){
                this.width = this.img.width;
                this.height = this.img.height;
            }else{
                this.width = this.height / this.img.height * this.img.width;
            }
        }else{
            if(!this.height) this.height = this.width / this.img.width * this.img.height;
        }
        var imgSize = a.setImgSize({
            w:this.img.width,
            h:this.img.height,
            _w:this.width,
            _h:this.height
        })
        this.left = imgSize[3];
        this.top = imgSize[2];
        this.scale = imgSize[4];
    }
    this.comput = function(){
        var imgSize = a.setImgSize({w:this.width,h:this.height,_w:this.maxWidth,_h:this.maxHeight});
        this.canvas.width = this.width;
        this.canvas.height = this.height;
        this.canvas.style.transform = 'scale(' + imgSize[4] + ',' + imgSize[4] + ')';
        this.canvas.style.top = imgSize[2] + 'px';
        this.canvas.style.left = imgSize[3] + 'px';
        this.canvas.style.background = 'url()';
        
        if(this.fillStyle){
            this.ctx.fillStyle = this.fillStyle;
            this.ctx.fillRect(0,0,this.width,this.height);
        }

        //圆角
        if(this.radius){
            //左上
            var site = [[this.radius,0],[0,0],[0,this.radius]];
            this.ctx.moveTo(site[0][0],site[0][1]);
            this.ctx.arcTo(site[1][0],site[1][1],site[2][0],site[2][1],this.radius);
            //左下
            site = [[0,this.height - this.radius],[0,this.height],[this.radius,this.height]];
            this.ctx.lineTo(site[0][0],site[0][1]);
            this.ctx.arcTo(site[1][0],site[1][1],site[2][0], site[2][1],this.radius);
            //右下
            site = [[this.width - this.radius,this.height],[this.width,this.height],[this.width,this.height - this.radius]];
            this.ctx.lineTo(site[0][0],site[0][1]);
            this.ctx.arcTo(site[1][0],site[1][1],site[2][0], site[2][1],this.radius);
            //右上
            site = [[this.width,this.radius],[this.width,0],[this.width - this.radius,0]];
            this.ctx.lineTo(site[0][0],site[0][1]);
            this.ctx.arcTo(site[1][0],site[1][1],site[2][0], site[2][1],this.radius);
            this.ctx.closePath();
            this.ctx.clip();
        }
        this.ctx.translate(this.left,this.top);
        this.ctx.drawImage(this.img,0,0,this.img.width * this.scale,this.img.height * this.scale);
    }
    this.show = function(){
        this.recommend();
        var canvasBox = document.getElementById('canvasBox' + this.id);
        canvasBox.style.height = this.maxHeight + 'px';
        this.canvas = a.creatElement('canvas',{
            width:300,
            height:300,
            style:{
                position:'relative',
                transformOrigin: 'left top',
                cursor:'move'
            },
            parent:canvasBox
        });
        function scrollFunc(e){
            e.preventDefault();
            if((e.wheelDelta && e.wheelDelta > 0) || (e.detail && e.detail < 0)){
                _this.scale += 0.02;
            }else{
                _this.scale -= 0.02;
            }
            _this.comput();
            _this.getData();
        }
        a.on(this.canvas,'mousewheel',scrollFunc);
        a.on(this.canvas,'DOMMouseScroll',scrollFunc);
        a.on(this.canvas,'move',function(e){
            _this.left = e.xyz._x;
            _this.top = e.xyz._y;
            _this.comput();
        },{
            start:function(){
                return [_this.left,_this.top];
            },
            end:function(){
                _this.getData();
            }
        });

        this.ctx = this.canvas.getContext("2d");
        this.comput();
        if(args.unEditable) return false;
        var editBox = document.getElementById('editBox' + this.id);
        var inputStyle = {
            border:'1px solid #e4e4e4',
            height:'20px',
            width:'100px',
            margin:'8px',
            padding:'3px 8px',
            fontSize:'14px'
        }
        var widthInput = a.creatElement('input',{
            type:'number',
            attr:{
                placeholder:'宽'
            },
            style:inputStyle,
            value:this.width,
            parent:editBox,
            onblur:function(){
                if(this.value > 0){
                    _this.width = this.value;
                    _this.comput();
                    _this.getData();
                }else{
                    this.value = _this.width;
                }
            }
        })
        var heihtInput = a.creatElement('input',{
            type:'number',
            attr:{
                placeholder:'高'
            },
            style:inputStyle,
            value:this.height,
            parent:editBox,
            onblur:function(){
                if(this.value > 0){
                    _this.height = this.value;
                    _this.comput();
                    _this.getData();
                }else{
                    this.value = _this.height;
                }
            }
        })
        var radiusInput = a.creatElement('input',{
            type:'number',
            attr:{
                placeholder:'圆角'
            },
            style:inputStyle,
            value:this.radius,
            parent:editBox,
            onblur:function(){
                if(this.value >= 0){
                    _this.radius = this.value;
                    _this.comput();
                    _this.getData();
                }else{
                    this.value = _this.radius;
                }
            }
        })
        var radiusInput = a.creatElement('button',{
            innerHTML:'推荐',
            style:'width:60px;height:24px;overflow:hidden;text-align:center;line-height:24px;border:1px solid #ccc;background:none;font-size:14px;',
            parent:editBox,
            onclick:function(){
                _this.recommend();
                _this.comput();
                _this.getData();
            }
        })
    }
    this.getData = function(){
        var url = this.canvas.toDataURL("image/" + this.mimeType,this.compress);
        var blob = a.UrlToBlob(url);
        this.canvas.title = '大小:' + this.fileSize(blob.size) + ',原图比例:' + parseInt(this.scale * 10000) / 100 + '%';
        return {
            url:url,
            blob:a.UrlToBlob(url),
            name:this.name.replace(/\.\w+$/,'') + '.' + this.mimeType
        }
    }
    this.open = function(imgUrl,next,_args){
        if(_args) a.merge(this,_args);
        a.Modal.modal({
            title:'编辑' + (this.name || '图片'),
            content:'<div id="canvasBox' + this.id + '" style="background:#000;text-align:left;"></div><div style="text-align:left;" id="editBox' + this.id + '"></div>',
            boxStyle:{
                width:this.maxWidth + 'px'
            },
            styles:{
                modalContent:'padding:0px;'
            },
            success:function(ret){
                next(ret.confirm ? _this.getData() : {errorMsg:'用户取消'});
            }
        })
        this.img.onload = function(){
            if(!(_this.width > 0)) _this.width = this.width;
            if(!(_this.height > 0)) _this.height = this.height;
            _this.recommend();
            _this.show();
            if(typeof args.afterOpen == 'function') args.afterOpen.call(_this);
        }
        this.img.src = imgUrl;
    }
}
//ImgEdit end
//mouseOverShow begin
a.mouseOverShow = function(mouseoverObj,showObj,args){
    args = args || {};
    var id = a.zIndex(),timer = 0;
    a.on(mouseoverObj,'mouseover',function(){
        if(timer) {
            clearTimeout(timer);
        }else{
            var rect = a.getBoundingClientRect(mouseoverObj);
            if(args.string) {
                var top = rect.top + rect.height + 8;
                var left = rect.left;
                showObj = a.creatElement('div',{
                    id:'mouseOverShow' + id,
                    className:'mouseOverShow',
                    style:'position:fixed;left:' + left + 'px;top:' + top + 'px;border-radius:5px;width:180px;padding:16px 0px 16px 16px;background:#000;color:#fff;line-height:24px;background:#000;color:#fff;z-index:{zIndex}'._eval({zIndex:a.zIndex(4)}) + (args.style || ''),
                    innerHTML:args.string,
                    parent:true
                })
                if(args.init) args.init(id);
                rect = mouseoverObj.getBoundingClientRect();
                if(rect.height + top > a.body.clientWidth) {
                    showObj.style.top = 'auto';
                    showObj.style.bottom = '0px';
                }
                if(rect.height + left > a.body.clientHeight) {
                    showObj.style.left = 'auto';
                    showObj.style.right = '0px';
                }
                showObjBind();
            }else{
                showObj.style.display = 'block';
            }
            if(args.showFun) args.showFun.call(null,mouseoverObj,showObj,rect);
        }
    })
    a.on(mouseoverObj,'mouseout',function(){
        timer = setTimeout(function(){
            if(args.string) {
                showObj.parentNode.removeChild(showObj);
                showObj = null;
            }else{
                showObj.style.display = 'none';
            }
            if(args.hideFun) args.hideFun.call(null,mouseoverObj,showObj);
            timer = 0;
        },300)
    })
    if(showObj) showObjBind();
    function showObjBind(){
        a.on(showObj,'mouseover',function(){
            clearTimeout(timer);
        })
        a.on(showObj,'mouseout',function(){
            timer = setTimeout(function(){
                if(args.string) {
                    showObj.parentNode.removeChild(showObj);
                    showObj = null;
                }else{
                    showObj.style.display = 'none';
                }
                if(args.hideFun) args.hideFun.call(null,mouseoverObj,showObj);
                timer = 0;
            },300)
        })
    }
}
//mouseOverShow end